package com.jt.www.common.controller;

import com.jt.www.annotations.AvoidRepeatableCommit;
import com.jt.www.common.service.GradeService;
import com.jt.www.model.grade.Grade;
import com.jt.www.model.grade.vo.GradeVo;
import com.jt.www.model.reps.GenericDataResponse;
import com.jt.www.model.reps.ResultDTO;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Api(value = "年级信息接口", tags = {"GRADE-INFO-API"}, description = "年级信息接口")
@RestController
@RequestMapping(value = "/gradeinfo")
public class GradeController {

    public static final Logger log = LoggerFactory.getLogger(GradeController.class);

    @Autowired
    private GradeService gradeService;

    /**
     * 新增年级信息
     */

    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "X-RESUB-TOKEN", value = "token", required = true)})
    @ApiOperation(value = "新增信息", notes = "新增信息", httpMethod = "POST")
    @AvoidRepeatableCommit
    @PostMapping("/addGradeInfo")
    public ResponseEntity insertGradeInfo(@RequestBody Grade grade) {
        Integer num = gradeService.insertGradeInfo(grade);
        if (num == 1) {
            return GenericDataResponse.ok();
        } else if (num == -1) {
            return GenericDataResponse.errorWithMsg("参数传入不准确");
        } else if (num == -2) {
            return GenericDataResponse.errorWithMsg("改年级信息已存在, 不能重复添加");
        }

        return GenericDataResponse.errorWithMsg("添加失败");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "年级删除消息", notes = "年级删除消息", httpMethod = "GET")
    @GetMapping(value = "/deleteGrade")
    public ResponseEntity deleteGradeById(@ApiParam(value = "消息id", required = true) @RequestParam("id") String id) {

        if (StringUtils.isEmpty(id)) {
            log.error("【删除消息】id为空");
            return GenericDataResponse.errorWithMsg("参数传入不正确");
        }

        int num = gradeService.deleteGradeById(Integer.valueOf(id));
        if (num == 1) {
            return GenericDataResponse.ok();
        }
        return GenericDataResponse.errorWithMsg("修改失败");
    }

    /**
     * 修改年级消息
     */
    @ApiOperation(value = "修改年级信息", notes = "修改年级信息", httpMethod = "POST")
    @PostMapping(value = "/update")
    public ResponseEntity updateSchoolInfo(@RequestBody Grade grade) {

        Integer num = gradeService.updateGradeInfo(grade);
        if (num == 1) {
            return GenericDataResponse.ok();
        } else if (num == -1) {
            return GenericDataResponse.errorWithMsg("参数不能为空");
        }
        return GenericDataResponse.errorWithMsg("修改失败");
    }

    /**
     * 根据年级名称，班级数量查询年级信息
     */
    @ApiOperation(value = "根据年级名称，班级数量查询年级信息", notes = "根据年级名称，班级数量查询年级信息", httpMethod = "GET")
    @GetMapping(value = "/getGradeByNameNumber")
    public ResultDTO searchSchoolByNameAddressPhone(GradeVo grade) {

        ResultDTO resultMap = gradeService.searchGradeByNameNumber(grade);
        return resultMap;
    }


}
